package com.quanyue.music.system.service.impl;

import com.quanyue.auto.cache.CacheService;
import com.quanyue.music.domain.constant.CacheConstants;
import com.quanyue.music.system.service.SplitMp3Service;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class SplitMp3ServiceImpl implements SplitMp3Service {

    @Autowired
    private CacheService cacheService;

    @Override
    public List<MultipartFile> split(MultipartFile file, String uuid, String localPath) {
        List<MultipartFile> list = new ArrayList<>();
        String fileName = uuid + ".mp3";
        try {
            File check = new File(localPath);
            if (!check.exists()) {
                check.mkdirs();
            }
            // 转换MultipartFile为File
            File f = new File(localPath + fileName);
            FileUtils.copyInputStreamToFile(file.getInputStream(), f);

            // 输出流读取字节数存入分割小文件
            // 18mb
            File outFile1 = new File(localPath, "split1-" + fileName);
            // 9mb
            File outFile2 = new File(localPath, "split2-" + fileName);

            // 输入流读取源文件
            FileInputStream inputStream = new FileInputStream(f);

            // 建立输出流写入源文件指定字节数
            FileOutputStream outputStream1 = new FileOutputStream(outFile1);
            FileOutputStream outputStream2 = new FileOutputStream(outFile2);

            // 定义输入流每次读取字节数
            byte[] buf = new byte[file.getBytes().length / 3];
            int length = 0;
            // 记录写入的字节流长度
            int count = 0;
            // 记录写入次数
            int writeCount = 0;
            while ((length = inputStream.read(buf)) != -1) {
                count = count + length;
                if (writeCount == 1) {
                    outputStream1.write(buf);
                } else if (writeCount == 2) {
                    outputStream1.write(buf);
                    outputStream2.write(buf);
                }
                ++writeCount;
            }
            // 封装返回值  需要上传到第三方服务器时解开
            list.add(new MockMultipartFile(f.getName(), new FileInputStream(f)));
            list.add(new MockMultipartFile(outFile1.getName(), new FileInputStream(outFile1)));
            list.add(new MockMultipartFile(outFile2.getName(), new FileInputStream(outFile2)));
            // 关闭流
            inputStream.close();
            outputStream1.close();
            outputStream2.close();
            return list;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @PostConstruct
    public void initFileCache() {
        Map<String, byte[]> map = new ConcurrentHashMap<>();
        cacheService.put(CacheConstants.FILE_CACHE.name(), map);
    }
}
